package cn.fl.extcon.util;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

/**
 * 数字转大写
 */
public class FinaceNumber {
	private static Logger logger = Logger.getLogger(FinaceNumber.class);
	public static void main(String[] args) {
		//System.out.println(FinaceNumber.digitUppercase(571300));
		System.out.println(FinaceNumber.number2CNMontrayUnit(new BigDecimal ("5808")));
	}
	/**
	 * 数字转大写
	 * 
	 * @param n
	 * @return
	 */
	public static String digitUppercase(double n) {
		String fraction[] = { "角", "分" };
		String digit[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
		String unit[][] = { { "元", "万", "亿" }, { "", "拾", "佰", "仟" } };

		String head = n < 0 ? "负" : "";
		n = Math.abs(n);

		String s = "";
		for (int i = 0; i < fraction.length; i++) {
			s += (digit[(int) (Math.floor(n * 10 * Math.pow(10, i)) % 10)] + fraction[i])
					.replaceAll("(零.)+", "");
		}
		if (s.length() < 1) {
			s = "整";
		}
		int integerPart = (int) Math.floor(n);

		for (int i = 0; i < unit[0].length && integerPart > 0; i++) {
			String p = "";
			for (int j = 0; j < unit[1].length && n > 0; j++) {
				p = digit[integerPart % 10] + unit[1][j] + p;
				integerPart = integerPart / 10;
			}
			s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i]
					+ s;
		}
		return head
				+ s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "")
						.replaceAll("(零.)+", "零").replaceAll("^整$", "零元整");
	}

	/**
	 * 小写转换为大写 参数名将变为：将原小写数字的参数名转为大写，然后再其后面添加"_D"
	 * 
	 * @param map
	 * @return
	 */
	public static HashMap<String,Object> numToUpCase(Map<String, Object> map) {
		System.out.println(map);
		Set<String> set = map.keySet();
		System.out.println("set="+set);
		HashMap<String,Object> mm=new HashMap<String,Object>();
		for (String key : set) {
			if (key == null || key.length() <= 0) {
				continue;
			} else {
				String str = null == map.get(key) ? "" : map.get(key)
						.toString();
				if (isDouble(str)) {
					double str_d = Double.parseDouble(str);
					String rst = FinaceNumber.digitUppercase(str_d);
					mm.put(key, str);
					mm.put(key.toUpperCase() + "_D", rst);
					mm.put(key.toUpperCase()+"大写值", rst);
				}else{
					mm.put(key, str);
				}
			}
		}
		return mm;
	}

	private static boolean isDouble(String str) {
		try {
			Double.parseDouble(str);
			return true;

		} catch (Exception ex) {
			logger.error(ex);
			return false;
		}

	}
	/**
     * 把输入的金额转换为汉语中人民币的大写
     * @author LvYu 
     * @param numberOfMoney
     *            输入的金额
     * @return 对应的汉语大写
     * @date 2017年2月24日下午15:47:58
     */
    public static String number2CNMontrayUnit(BigDecimal numberOfMoney) {
    	/**
         * 汉语中数字大写
         */
        String[] CN_UPPER_NUMBER = { "零", "壹", "贰", "叁", "肆",
                "伍", "陆", "柒", "捌", "玖" };
        /**
         * 汉语中货币单位大写，这样的设计类似于占位符
         */
        String[] CN_UPPER_MONETRAY_UNIT = { "分", "角", "元",
                "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾",
                "佰", "仟" };
        /**
         * 特殊字符：整
         */
        String CN_FULL = "整";
        /**
         * 特殊字符：负
         */
        String CN_NEGATIVE = "负";
        /**
         * 金额的精度，默认值为2
         */
        int MONEY_PRECISION = 2;
        /**
         * 特殊字符：零元整
         */
        String CN_ZEOR_FULL = "零元" + CN_FULL;
        StringBuffer sb = new StringBuffer();
        // -1, 0, or 1 as the value of this BigDecimal is negative, zero, or
        // positive.
        int signum = numberOfMoney.signum();
        // 零元整的情况
        if (signum == 0) {
            return CN_ZEOR_FULL;
        }
        //这里会进行金额的四舍五入
        long number = numberOfMoney.movePointRight(MONEY_PRECISION)
                .setScale(0, 4).abs().longValue();
        // 得到小数点后两位值
        long scale = number % 100;
        int numUnit = 0;
        int numIndex = 0;
        boolean getZero = false;
        // 判断最后两位数，一共有四中情况：00 = 0, 01 = 1, 10, 11
        if (!(scale > 0)) {
            numIndex = 2;
            number = number / 100;
            getZero = true;
        }
        if ((scale > 0) && (!(scale % 10 > 0))) {
            numIndex = 1;
            number = number / 10;
            getZero = true;
        }
        int zeroSize = 0;
        while (true) {
            if (number <= 0) {
                break;
            }
            // 每次获取到最后一个数
            numUnit = (int) (number % 10);
            if (numUnit > 0) {
                if ((numIndex == 9) && (zeroSize >= 3)) {
                    sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
                }
                if ((numIndex == 13) && (zeroSize >= 3)) {
                    sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
                }
                sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
                sb.insert(0, CN_UPPER_NUMBER[numUnit]);
                getZero = false;
                zeroSize = 0;
            } else {
                ++zeroSize;
                if (!(getZero)) {
                    sb.insert(0, CN_UPPER_NUMBER[numUnit]);
                }
                if (numIndex == 2) {
                   if (number > 0) {
                       sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
                   }
               } else if (((numIndex - 2) % 4 == 0) && (number % 1000 > 0)) {
                   sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
               }
					getZero = true;
           }
           // 让number每次都去掉最后一个数
           number = number / 10;
           ++numIndex;
       }
       // 如果signum == -1，则说明输入的数字为负数，就在最前面追加特殊字符：负
       if (signum == -1) {
           sb.insert(0, CN_NEGATIVE);
       }
       // 输入的数字小数点后两位为"00"的情况，则要在最后追加特殊字符：整
       if (!(scale > 0)) {
           sb.append(CN_FULL);
       }
       return sb.toString();
   }
}
